#include"GameWithComputer.h"
Board::taken(int x,int y,bool black)
{//returns how many more pieces there would be if a piece is put at x,y
	int tempBlack=count[BLACK];
	int tempWhite=count[WHITE];
	for(int i(0); i<10; i++){
		for(int j(0); j<10; j++){
			temp[i][j]=board[i][j];
		}
	}
	int result=PutPiece(black, x, y);
	if(result==0){
		return 0;
	}
	for(int i(0); i<10; i++){
		for(int j(0); j<10; j++){
			board[i][j]=temp[i][j];
		}
	}
	result=abs(count[BLACK]-tempBlack);
	count[BLACK]=tempBlack;

	count[WHITE]=tempWhite;
	return result;

}
Board::Reset()
{
	for(int i(0); i<10; i++){//we only use 1 to 8
		for(int j(0); j<10; j++){//we only use 1 to 8
			board[i][j].Initialize();
			temp[i][j].Initialize();
		}
	}
	board[5][5].PutPiece(WHITE);
	board[4][4].PutPiece(WHITE);
	board[5][4].PutPiece(BLACK);
	board[4][5].PutPiece(BLACK);
	count[WHITE]=count[BLACK]=2;
}

Board::isLegal(int x,int y,bool black)
{
	if(board[x][y].isOn()) return false;
	if(board[x-1][y].isOn()&&board[x-1][y].isBlack()==!black){
		for(int i(x-2); ; i--){
			if(board[i][y].isOn()){
				if(board[i][y].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	if(board[x+1][y].isOn()&&board[x+1][y].isBlack()==!black){
		for(int i(x+2); ; i++){
			if(board[i][y].isOn()){
				if(board[i][y].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	if(board[x][y-1].isOn()&&board[x][y-1].isBlack()==!black){
		for(int i(y-2); ; i--){
			if(board[x][i].isOn()){
				if(board[x][i].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}

	if(board[x][y+1].isOn()&&board[x][y+1].isBlack()==!black){
		for(int i(y+2); ; i++){
			if(board[x][i].isOn()){
				if(board[x][i].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}

	if(board[x+1][y+1].isOn()&&board[x+1][y+1].isBlack()==!black){
		for(int i(x+2), j(y+2); ; i++, j++){
			if(board[i][j].isOn()){
				if(board[i][j].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	if(board[x+1][y-1].isOn()&&board[x+1][y-1].isBlack()==!black){
		for(int i(x+2), j(y-2); ; i++, j--){
			if(board[i][j].isOn()){
				if(board[i][j].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	if(board[x-1][y+1].isOn()&&board[x-1][y+1].isBlack()==!black){
		for(int i(x-2), j(y+2); ; i--, j++){
			if(board[i][j].isOn()){
				if(board[i][j].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	if(board[x-1][y-1].isOn()&&board[x-1][y-1].isBlack()==!black){
		for(int i(x-2), j(y-2); ; i--, j--){
			if(board[i][j].isOn()){
				if(board[i][j].isBlack()==black){
					return true;
				}
			}
			else break;
		}
	}
	return false;

}
Board::PutPiece(bool black,int x, int y)
{
	if(board[x][y].isOn()){
		return false;
	}
	if(x==0||x==9||y==0||y==9){
		return false;
	}
	int successfullyPut(0), broke(0);
	int i;
	for(i=1; ;i++){
		if(board[x+i][y].isOn()&&board[x+i][y].isBlack()!=black){
			board[x+i][y].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x+i][y].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x+i][y].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}



	for(i=1; ;i++){
		if(board[x-i][y].isOn()&&board[x-i][y].isBlack()!=black){
			board[x-i][y].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x-i][y].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x-i][y].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}




	for(i=1; ;i++){
		if(board[x][y+i].isOn()&&board[x][y+i].isBlack()!=black){
			board[x][y+i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x][y+i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x][y+i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}




	for(i=1; ;i++){
		if(board[x][y-i].isOn()&&board[x][y-i].isBlack()!=black){
			board[x][y-i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x][y-i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x][y-i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}




	for(i=1; ;i++){
		if(board[x+i][y+i].isOn()&&board[x+i][y+i].isBlack()!=black){
			board[x+i][y+i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x+i][y+i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x+i][y+i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}





	for(i=1; ;i++){
		if(board[x-i][y+i].isOn()&&board[x-i][y+i].isBlack()!=black){
			board[x-i][y+i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x-i][y+i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x-i][y+i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}

	for(i=1; ;i++){
		if(board[x+i][y-i].isOn()&&board[x+i][y-i].isBlack()!=black){
			board[x+i][y-i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x+i][y-i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x+i][y-i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}



	for(i=1; ;i++){
		if(board[x-i][y-i].isOn()&&board[x-i][y-i].isBlack()!=black){
			board[x-i][y-i].Reverse();
			count[black]++;
			count[!black]--;
			successfullyPut++;
		}
		else{
			broke++;
			break;
		}
	}
	if(broke&&!board[x-i][y-i].isOn()){
		broke--;
		for(i--; i>0; i--){
			board[x-i][y-i].Reverse();
			count[black]--;
			count[!black]++;
			successfullyPut--;
		}
	}

	if(successfullyPut){
		board[x][y].PutPiece(black);
		count[black]++;
	}
	
	return successfullyPut;
}